#!/bin/csh           

# This script converts model data into FUA/FSF files 

# Supports composite reflectivity data for the HRRR, and can be more 
# generalized later to other fields/models

uname -a

setenv LAPS_DATA_ROOT $1
setenv MODELS "$2"

setenv LAPSINSTALLROOT @prefix@

# Model Delay in hours - if length of string is 9, then explicit YYDDHHMM is being entered
if ($3 == "") then
    setenv DELAY 3
else
    setenv DELAY $3
endif

setenv REMOTE_DATA_ROOT $4

setenv PROC $$

if (-e /public) then
    setenv GSDPUBLIC /public
else
    setenv GSDPUBLIC /scratch2/portfolios/BMC/public
endif

echo "GSDPUBLIC is $GSDPUBLIC"

# Obtain model runtime (on the hour), default is with a 3 hour delay
@ LEN_DELAY = `echo $DELAY | wc -c` - 1
if ($LEN_DELAY == 9) then
    setenv YYDDDHHMM $DELAY
else
    setenv YYDDDHHMM `perl @prefix@/etc/sched_sys.pl -d $DELAY -c 3600`              
endif

echo "YYDDDHHMM (model initialization time to process) = $YYDDDHHMM"

setenv FCSTINTVL `perl $LAPSINSTALLROOT/etc/read_nl.pl -d $LAPS_DATA_ROOT -n nest7grid.parms -v model_fcst_intvl`
echo "FCSTINTVL is $FCSTINTVL"

echo "Inputted MODEL(s): $MODELS"

foreach MODEL ($MODELS)

  echo "Processing model $MODEL"

  if ($MODEL == wrf-hrrr) then
    setenv CMODEL HRRR
    setenv MODE_FSF WGRIB
    if ($FCSTINTVL <= 3600) then
        setenv HHTIMES '00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15'
    else
        setenv HHTIMES '00 03 06 09 12 15'
    endif
  else if ($MODEL == nam) then
    setenv CMODEL NAM
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/nam/A218/grib2 
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84'                                      
    setenv MMTIMES '00'
  else if ($MODEL == nam-nh) then
    setenv CMODEL NAM
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/nam/nh221/grib2 
    setenv HHTIMES '00 06 12 18 24 30 36 42 48 54 60 66 72 78 84'                                      
    setenv MMTIMES '00'
  else if ($MODEL == fim) then
    setenv CMODEL FIM
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/gsd/fim9/0p125/grib2
    setenv HHTIMES '00 06 12 18 24 30 36 42 48 54 60 66 72 78 84'                                      
    setenv MMTIMES '00'
  else if ($MODEL == rr) then
    setenv CMODEL RR
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/rap/iso_130/grib2
    if ($FCSTINTVL <= 3600) then
        setenv HHTIMES '00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18'
    else
        setenv HHTIMES '00 03 06 09 12 15 18'
    endif
    setenv MMTIMES '00'
  else if ($MODEL == rap-nh) then
    setenv CMODEL RR 
    setenv MODE_FSF WGRIB # REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/gsd/rap/83_full/wrftwo 
    if ($FCSTINTVL <= 3600) then
        setenv HHTIMES '00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18'
    else
        setenv HHTIMES '00 03 06 09 12 15 18'
    endif
    setenv MMTIMES '00'
  else if ($MODEL == wndw-warw) then
    setenv CMODEL WNDW-WARW
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/hiresw/westarw/grib2
    if ($FCSTINTVL <= 3600) then
        setenv HHTIMES '00 01 02 03 04 05 06 07 08 09 10 11 12'
    else
        setenv HHTIMES '00 03 06 09 12'
    endif
    setenv MMTIMES '00'
  else if ($MODEL == em-ctl) then
    setenv CMODEL EM-CTL
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.ctl
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  else if ($MODEL == em-n1) then
    setenv CMODEL EM-N1
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.n1
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  else if ($MODEL == em-n2) then
    setenv CMODEL EM-N2
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.n2
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  else if ($MODEL == em-n3) then
    setenv CMODEL EM-N3
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.n3
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  else if ($MODEL == em-p1) then
    setenv CMODEL EM-P1
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.p1
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  else if ($MODEL == em-p2) then
    setenv CMODEL EM-P2
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.p2
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  else if ($MODEL == em-p3) then
    setenv CMODEL EM-P3
    setenv MODE_FSF REGRID
    setenv GRIBINDIR $GSDPUBLIC/data/grids/sref/em.p3
    setenv HHTIMES '00 03 06 09 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87'                                      
    setenv MMTIMES '00'
  endif

  echo "CMODEL is $CMODEL"
  echo "MODE_FSF is $MODE_FSF"

  setenv OUTDIR_FUA $LAPS_DATA_ROOT/lapsprd/fua/$MODEL
  setenv OUTDIR_FSF $LAPS_DATA_ROOT/lapsprd/fsf/$MODEL

  mkdir -p $OUTDIR_FUA
  mkdir -p $OUTDIR_FSF

  setenv FCSTLEN_MM  `perl $LAPSINSTALLROOT/etc/read_nl.pl -d $LAPS_DATA_ROOT -n nest7grid.parms -v model_fcst_len`
  echo "FCSTLEN_MM is $FCSTLEN_MM"

  @ FCSTLEN_HH = $FCSTLEN_MM / 60
  echo "FCSTLEN_HH is $FCSTLEN_HH"

# if (-e /data/fab/laps/laps_conus.fsf) then
#   setenv NCTEMPLATE /data/fab/laps/laps_conus.fsf
# endif

  setenv NCTEMPLATE_FSF $LAPS_DATA_ROOT/template.fsf
  setenv NCTEMPLATE_FUA $LAPS_DATA_ROOT/template.fua

# Create FSF template file
#echo "ncgen -o $NCTEMPLATE $LAPS_DATA_ROOT/cdl/fsf.cdl"
#      ncgen -o $NCTEMPLATE $LAPS_DATA_ROOT/cdl/fsf.cdl

  if ($MODE_FSF == WGRIB) then
      echo "NCTEMPLATE_FSF is $NCTEMPLATE_FSF"
      ls -l $NCTEMPLATE_FSF

      if (! -e $NCTEMPLATE_FSF) then
        echo "WARNING $NCTEMPLATE_FSF does not exist - create template"
        ncgen -o $NCTEMPLATE_FSF $LAPS_DATA_ROOT/cdl/fsf.cdl
      endif
  endif

  foreach FCST_HH ($HHTIMES)                                     

   setenv FCST_HH_INT `echo $FCST_HH | sed 's/^0//'` # remove leading zero
 
   if ($FCST_HH_INT <= $FCSTLEN_HH) then

    if ($CMODEL == HRRR) then

      if ($FCSTINTVL == 900 || $FCSTINTVL == 1800) then
        echo "using 15 minute output"

        if ($FCST_HH == 00) then
            setenv MMTIMES '00'
            setenv FILE_HH 00          
        else
            setenv MMTIMES '15 30 45 60'
        endif

      else
        echo "using 15 minute output files, for every 60 minutes of output"

        if ($FCST_HH == 00) then
            setenv MMTIMES '00'
            setenv FILE_HH 00          
        else
            if ($CMODEL == HRRR) then
                setenv MMTIMES '60'
            else
                setenv FILE_HH $FCST_HH
                setenv MMTIMES '00'
            endif
        endif

      endif

    else # other model
      setenv FILE_HH $FCST_HH

    endif

    foreach FMM ($MMTIMES)

      if ($MODE_FSF == WGRIB) then

        if ($FMM == 60 || $FCST_HH == 00) then
            setenv FILE_HH $FCST_HH
            setenv FCSTTIME $FILE_HH\00 
        else
            @ FHH_I = $FCST_HH - 1
            if ($FHH_I < 10) then
                setenv FILE_HH 0$FHH_I
            else
                setenv FILE_HH $FHH_I
            endif
            setenv FCSTTIME $FILE_HH$FMM
        endif

        setenv NTABLE 32

        if ($FCST_HH == 00) then
            @ REC_LMR = 1
        else
            @ REC_LMR = ((($FMM / 15) - 1) * $NTABLE) + 1
        endif

#       Info from HRRR 15-min GRIB2 table - http://ruc.noaa.gov/hrrr/GRIB2Table-subh.txt
        @ REC_PSF = $REC_LMR + 17 # 18 in table
        @ REC_TSF = $REC_LMR + 18 # 19 in table
        @ REC_DSF = $REC_LMR + 20 # 20 in table
        @ REC_WSF = $REC_LMR + 21 # 21 in table
        @ REC_SWI = $REC_LMR + 30 # 30 in table
#       @ REC_RTO = 52                                    

        echo " "
        echo "Processing forecast time/record $FCSTTIME $REC_LMR"

#       setenv GRIBIN /home/Steve.Albers$GSDPUBLIC/data/fsl/hrrr/conus/wrftwo_subh/$YYDDDHHMM$FCST_HH\00         
        if ($MODEL == wrf-hrrr) then
            setenv GRIBIN $GSDPUBLIC/data/fsl/hrrr/conus/wrftwo_subh/$YYDDDHHMM$FCST_HH\00         
        else
            setenv GRIBIN $GRIBINDIR/$YYDDDHHMM$FCST_HH\00         
        endif

#       setenv TMPOUT_FSF /home/Steve.Albers/outdir/$YYDDDHHMM$FCSTTIME.fsf
        setenv NCOUT_FSF $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME.fsf
        setenv NCOUT_FUA $OUTDIR_FUA/$YYDDDHHMM$FCSTTIME.fua

#       Consider a local disk for faster intermediate file processing, otherwise use a dot file
        if (-e /export/scratch/null) then
            setenv TMPDIR /export/scratch/mdlpost
            mkdir -p $TMPDIR
            if (! -e $TMPDIR) then
                echo "ERROR: Could not create/find $TMPDIR - exiting..."
                exit
            endif
            perl $LAPSINSTALLROOT/etc/purger.pl -r -t 1.0 $TMPDIR
            setenv TMPOUT_FUA $TMPDIR/$YYDDDHHMM$FCSTTIME.fua.$PROC
            setenv TMPOUT_FSF $TMPDIR/$YYDDDHHMM$FCSTTIME.fsf.$PROC
        else
            setenv TMPOUT_FUA $OUTDIR_FUA/.$YYDDDHHMM$FCSTTIME.fua
            setenv TMPOUT_FSF $OUTDIR_FSF/.$YYDDDHHMM$FCSTTIME.fsf
        endif

#       Specify stmas_hwt grid for testing
#       setenv NEWGRID "-new_grid lambert:-98.408:35.25:35.25 -105.0:433:3000. 28.0:433.:3000."
        setenv NEWGRID " "                                                                         
        echo "NEWGRID is $NEWGRID"

        if (! -e $GRIBIN) then
            echo "ERROR: $GRIBIN is not present"

        else          
#           Parse navigation variables from grib2 file into environment variables
            echo "$GRIBIN is present"

            echo              "wgrib2  $GRIBIN -s -grid -d $REC_LMR"                                             
                               wgrib2  $GRIBIN -s -grid -d $REC_LMR                                              
                  setenv LAT1 `wgrib2  $GRIBIN -s -grid -d $REC_LMR | grep Lat1 | awk '{print $2}'`            
            echo "Lat1 is $LAT1"

#           Convert GRIB2 file into initial NetCDF file
            echo " "
            echo "converting LMR"
            echo "wgrib2  $GRIBIN -s -d $REC_LMR         $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_LMR         $NEWGRID -netcdf $TMPOUT_FSF
 
            echo " "
            echo "converting TSF"
            echo "wgrib2  $GRIBIN -s -d $REC_TSF -append $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_TSF -append $NEWGRID -netcdf $TMPOUT_FSF  

            echo " "
            echo "converting DSF"
            echo "wgrib2  $GRIBIN -s -d $REC_DSF -append $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_DSF -append $NEWGRID -netcdf $TMPOUT_FSF

            echo " "
            echo "converting USF"
            echo "wgrib2  $GRIBIN -s -d $REC_WSF.1 -append $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_WSF.1 -append $NEWGRID -netcdf $TMPOUT_FSF

            echo " "
            echo "converting VSF"
            echo "wgrib2  $GRIBIN -s -d $REC_WSF.2 -append $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_WSF.2 -append $NEWGRID -netcdf $TMPOUT_FSF

            echo " "
            echo "converting SWI"
            echo "wgrib2  $GRIBIN -s -d $REC_SWI -append $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_SWI -append $NEWGRID -netcdf $TMPOUT_FSF

            echo " "
            echo "converting PSF"
            echo "wgrib2  $GRIBIN -s -d $REC_PSF -append $NEWGRID -netcdf $TMPOUT_FSF"
                  wgrib2  $GRIBIN -s -d $REC_PSF -append $NEWGRID -netcdf $TMPOUT_FSF


            setenv GRIB_PCP_IN /nofile                                                         
            if ($FMM == 00 || $FMM == 60) then
                setenv GRIB_PCP_IN $GSDPUBLIC/data/fsl/hrrr/conus/wrftwo/$YYDDDHHMM$FCST_HH\00         
                if (-e $GRIB_PCP_IN) then
                    echo "$GRIB_PCP_IN is present"

#                   echo "wgrib2  $GRIB_PCP_IN -s -d $REC_RTO -append $NEWGRID -netcdf $TMPOUT_FSF"
#                         wgrib2  $GRIB_PCP_IN -s -d $REC_RTO -append $NEWGRID -netcdf $TMPOUT_FSF
              
                    echo "precip command for rto"
                    echo "wgrib2  $GRIB_PCP_IN -s -match APCP:surface:0 -append $NEWGRID -netcdf $TMPOUT_FSF"                   
                          wgrib2  $GRIB_PCP_IN -s -match APCP:surface:0 -append $NEWGRID -netcdf $TMPOUT_FSF                   
              
                    echo "precip command for tpw"
                    echo "wgrib2  $GRIB_PCP_IN -s -match PWAT           -append $NEWGRID -netcdf $TMPOUT_FSF"                   
                          wgrib2  $GRIB_PCP_IN -s -match PWAT           -append $NEWGRID -netcdf $TMPOUT_FSF                   
              
                    echo "new precip command for r01"
                    if (-e $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.r01) then
                        if ($FCST_HH == 01 && $FCSTINTVL == 3600) then
                            echo "wgrib2  $GRIB_PCP_IN -s -match APCP:surface: | grep 0-1     | wgrib2 -i $GRIB_PCP_IN -nc_table $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.r01 -append $NEWGRID -netcdf $TMPOUT_FSF"                   
                                  wgrib2  $GRIB_PCP_IN -s -match APCP:surface: | grep 0-1     | wgrib2 -i $GRIB_PCP_IN -nc_table $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.r01 -append $NEWGRID -netcdf $TMPOUT_FSF
                        else
                            echo "wgrib2  $GRIB_PCP_IN -s -match APCP:surface: | grep -v "0-" | wgrib2 -i $GRIB_PCP_IN -nc_table $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.r01 -append $NEWGRID -netcdf $TMPOUT_FSF"                   
                                  wgrib2  $GRIB_PCP_IN -s -match APCP:surface: | grep -v "0-" | wgrib2 -i $GRIB_PCP_IN -nc_table $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.r01 -append $NEWGRID -netcdf $TMPOUT_FSF
                        endif

                    else
                        echo "ERROR: $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.r01 not present"

                    endif

                else
                    echo "$GRIB_PCP_IN is not present"
                endif
            endif

#           cp $NCOUT $NCOUT.wgrib2out

            ncdump -h $TMPOUT_FSF 
  
            if (-e $GRIB_PCP_IN) then
                echo "ncrename -v var0_16_196_localleveltype2000,lmr -v TMP_2maboveground,tsf -v DPT_2maboveground,dsf -v PRES_surface,psf -v UGRD_10maboveground,usf -v VGRD_10maboveground,vsf -v var0_4_192_surface,swi -v APCP_surface,rto -v PWAT_localleveltype2000,tpw $TMPOUT_FSF"          
                      ncrename -v var0_16_196_localleveltype2000,lmr -v TMP_2maboveground,tsf -v DPT_2maboveground,dsf -v PRES_surface,psf -v UGRD_10maboveground,usf -v VGRD_10maboveground,vsf -v var0_4_192_surface,swi -v APCP_surface,rto -v PWAT_localleveltype2000,tpw $TMPOUT_FSF           
 
            else
                echo "ncrename -v var0_16_196_localleveltype2000,lmr -v TMP_2maboveground,tsf -v DPT_2maboveground,dsf -v PRES_surface,psf -v UGRD_10maboveground,usf -v VGRD_10maboveground,vsf -v var0_4_192_surface,swi $TMPOUT_FSF"          
                      ncrename -v var0_16_196_localleveltype2000,lmr -v TMP_2maboveground,tsf -v DPT_2maboveground,dsf -v PRES_surface,psf -v UGRD_10maboveground,usf -v VGRD_10maboveground,vsf -v var0_4_192_surface,swi $TMPOUT_FSF           

            endif

            ncdump -h $TMPOUT_FSF 
#           cp $TMPOUT_FSF $TMPOUT_FSF.renamed

#           echo "ncatted -O -a lvl_coord,lmr,a,c,"MSL" $TMPOUT_FSF"
#                 ncatted -O -a lvl_coord,lmr,a,c,"MSL" $TMPOUT_FSF

#           echo "ncatted -O -a LAPS_units,lmr,a,c,"DBZ" $TMPOUT_FSF"
#                 ncatted -O -a LAPS_units,lmr,a,c,"DBZ" $TMPOUT_FSF

#           Add dimensions: record = UNLIMITED, z, nav, namelen                   
#           See http://jisao.washington.edu/data/nco/

#           Add record dimension
            echo "ncecat -O -h $TMPOUT_FSF $TMPOUT_FSF"
                  ncecat -O -h $TMPOUT_FSF $TMPOUT_FSF

#           Change value of time dimension (wipes out the lmr field)
#           setenv STR1 "time = UNLIMITED ; // (1 currently)"
#           setenv STR2 "time = 1 ;"
#           echo "ncdump $TMPOUT_FSF  | sed -e "s#^.$STR1# $STR2#" | ncgen -o $TMPOUT_FSF"
#                 ncdump $TMPOUT_FSF  | sed -e "s#^.$STR1# $STR2#" | ncgen -o $TMPOUT_FSF

#           Rename time dimension to z
            echo "ncrename -d time,z $TMPOUT_FSF"          
                  ncrename -d time,z $TMPOUT_FSF           

#           Add record dimension
#           echo "ncecat -O -h $TMPOUT_FSF $TMPOUT_FSF"       
#                 ncecat -O -h $TMPOUT_FSF $TMPOUT_FSF

#           Rename time dimension to nav
#           echo "ncrename -d time,nav $TMPOUT_FSF"          
#                 ncrename -d time,nav $TMPOUT_FSF           

#           Reorder dimensions                       
            echo "ncpdq -a record,z,y,x -O $TMPOUT_FSF $TMPOUT_FSF"
                  ncpdq -a record,z,y,x -O $TMPOUT_FSF $TMPOUT_FSF

#           Append miscellaneous variables from FSF template file
            echo "ncks -A -v lmr_comment,tsf_comment,dsf_comment,psf_comment,usf_comment,vsf_comment,swi_comment,tpw_comment,rto_comment,r01_comment,imax,jmax,kmax,kdim,level,lmr_fcinv,tsf_fcinv,dsf_fcinv,psf_fcinv,usf_fcinv,vsf_fcinv,swi_fcinv,tpw_fcinv,rto_fcinv,r01_fcinv,grid_type,x_dim,y_dim,Nx,Ny,Dx,Dy,La1,Lo1,LoV,La2,Lo2,Latin1,Latin2 $NCTEMPLATE_FSF $TMPOUT_FSF"
                  ncks -A -v lmr_comment,tsf_comment,dsf_comment,psf_comment,usf_comment,vsf_comment,swi_comment,tpw_comment,rto_comment,r01_comment,imax,jmax,kmax,kdim,level,lmr_fcinv,tsf_fcinv,dsf_fcinv,psf_fcinv,usf_fcinv,vsf_fcinv,swi_fcinv,tpw_fcinv,rto_fcinv,r01_fcinv,grid_type,x_dim,y_dim,Nx,Ny,Dx,Dy,La1,Lo1,LoV,La2,Lo2,Latin1,Latin2 $NCTEMPLATE_FSF $TMPOUT_FSF

            rm -f $TMPOUT_FSF.pid*nc*.tmp
#           cp $TMPOUT_FSF $TMPOUT_FSF.ncks

#           Modify the navigation variables
#           echo "ncap2 -O -v -s 'Dx=3000.' $TMPOUT_FSF $TMPOUT_FSF"
#                 ncap2 -O -v -s 'Dx=3000.' $TMPOUT_FSF $TMPOUT_FSF 

#           echo "ncatted -O -a Dy,global,m,f,3000. $TMPOUT_FSF"
#                 ncatted -O -a Dy,global,m,f,3000. $TMPOUT_FSF
        
#           echo "ncatted -O -a La1,global,m,f,$LAT1 $TMPOUT_FSF"
#                 ncatted -O -a La1,global,m,f,$LAT1 $TMPOUT_FSF

#           echo "ncatted -O -a Lo1,global,m,f,$LON1 $TMPOUT_FSF"
#                 ncatted -O -a Lo1,global,m,f,$LON1 $TMPOUT_FSF

        endif # GRIBIN file is present

      endif # MODE_FSF (WGRIB method)

      if ($CMODEL == HRRR) then
        if ($FMM == 00 || $FMM == 60) then
            echo " "
            date -u
            echo " "
            echo "Hourly HRRR files under construction for FUA"
            setenv GRIB_PRES_IN $GSDPUBLIC/data/fsl/hrrr/conus/wrfprs/$YYDDDHHMM$FCST_HH\00         
            setenv MODE_FUA WGRIB

            if (-e $GRIB_PRES_IN && -e $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.fua) then
                if ($MODE_FUA == WGRIB) then
                    echo "wgrib2 method...."
                    echo "wgrib2  $GRIB_PRES_IN -s | grep -e TMP -e DPT -e UGRD -e VGRD -e CLWMR -e CICE | grep mb: | wgrib2 -i $GRIB_PRES_IN -nc_table $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.fua $NEWGRID -netcdf $TMPOUT_FUA"                   
                          wgrib2  $GRIB_PRES_IN -s | grep -e TMP -e DPT -e UGRD -e VGRD -e CLWMR -e CICE | grep mb: | wgrib2 -i $GRIB_PRES_IN -nc_table $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.fua $NEWGRID -netcdf $TMPOUT_FUA                    

#                   echo "wgrib2  $GRIB_PRES_IN -s -match DPT | grep mb: | wgrib2 -i $GRIB_PRES_IN -append $NEWGRID -netcdf $NCOUT_FUA"                   
#                         wgrib2  $GRIB_PRES_IN -s -match DPT | grep mb: | wgrib2 -i $GRIB_PRES_IN -append $NEWGRID -netcdf $NCOUT_FUA                    

                    echo "output file..."
                    ls -l $TMPOUT_FUA

                    if (-e /export/scratch/null) then
                        echo "output directory..."
                        ls -l $TMPDIR             
                    endif
   
                    echo " "
                    date -u
                    echo " "

#                   Add record dimension
                    echo "ncecat -O -h $TMPOUT_FUA $TMPOUT_FUA"       
                          ncecat -O -h $TMPOUT_FUA $TMPOUT_FUA        

                    echo "rm -f $TMPOUT_FUA.pid*nc*.tmp"
                          rm -f $TMPOUT_FUA.pid*nc*.tmp

                    echo " "
                    date -u
                    echo " "

#                   Remove time dimension
                    echo "ncwa -O -a time $TMPOUT_FUA $TMPOUT_FUA"
                          ncwa -O -a time $TMPOUT_FUA $TMPOUT_FUA 

                    echo "rm -f $TMPOUT_FUA.pid*nc*.tmp"
                          rm -f $TMPOUT_FUA.pid*nc*.tmp

                    echo " "
                    date -u
                    echo " "

                    if (! -e $NCTEMPLATE_FUA) then
                        echo "WARNING $NCTEMPLATE_FUA does not exist - create template"
                        ncgen -o $NCTEMPLATE_FUA $LAPS_DATA_ROOT/cdl/fua.cdl
                    endif

                    if (-e $NCTEMPLATE_FUA) then
                        echo "ncks -A -v t3_comment,sh_comment,u3_comment,v3_comment,lwc_comment,ice_comment,imax,jmax,kmax,kdim,level,t3_fcinv,sh_fcinv,u3_fcinv,v3_fcinv,lwc_fcinv,ice_fcinv,grid_type,x_dim,y_dim,Nx,Ny,Dx,Dy,La1,Lo1,LoV,La2,Lo2,Latin1,Latin2 $NCTEMPLATE_FUA $TMPOUT_FUA"
                              ncks -A -v t3_comment,sh_comment,u3_comment,v3_comment,lwc_comment,ice_comment,imax,jmax,kmax,kdim,level,t3_fcinv,sh_fcinv,u3_fcinv,v3_fcinv,lwc_fcinv,ice_fcinv,grid_type,x_dim,y_dim,Nx,Ny,Dx,Dy,La1,Lo1,LoV,La2,Lo2,Latin1,Latin2 $NCTEMPLATE_FUA $TMPOUT_FUA 

                        echo "rm -f $TMPOUT_FUA.pid*nc*.tmp"
                              rm -f $TMPOUT_FUA.pid*nc*.tmp

                        echo " "
                        date -u
                        echo " "
                        echo "mv $TMPOUT_FUA $NCOUT_FUA"
                              mv $TMPOUT_FUA $NCOUT_FUA

                        echo " "
                        echo "mv $TMPOUT_FSF $NCOUT_FSF"
                              mv $TMPOUT_FSF $NCOUT_FSF

                        echo "** regridding $NCOUT_FSF and $NCOUT_FUA (by inputting $OUTDIR_FUA in arg 1) **"
                        echo "$LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FUA/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F T T wrf-hrrr $LAPS_DATA_ROOT"
                              $LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FUA/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F T T wrf-hrrr $LAPS_DATA_ROOT

                        if ($REMOTE_DATA_ROOT != "") then
                            cd $OUTDIR_FUA 
                            $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fua $REMOTE_DATA_ROOT/lapsprd/fua/$MODEL exchange
                        endif

                        if ($REMOTE_DATA_ROOT != "") then
                            cd $OUTDIR_FSF 
                            $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fsf $REMOTE_DATA_ROOT/lapsprd/fsf/$MODEL exchange
                        endif

                    else
                        echo "$NCTEMPLATE_FUA does not exist, skip rest of HRRR FUA processing"

                        echo " "
                        date -u
                        echo " "

                        echo "rm -f $TMPOUT_FUA.pid*nc*.tmp"
                              rm -f $TMPOUT_FUA.pid*nc*.tmp

                        echo "rm -f $TMPOUT_FUA"
                              rm -f $TMPOUT_FUA

                        echo " "
                        echo "mv $TMPOUT_FSF $NCOUT_FSF"
                              mv $TMPOUT_FSF $NCOUT_FSF

                        ls -l $NCOUT_FSF

                        echo " "

                        echo "*** regridding $NCOUT_FSF (by inputting $OUTDIR_FSF in arg 1) ***"
                        echo "$LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F F T wrf-hrrr $LAPS_DATA_ROOT"
                              $LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F F T wrf-hrrr $LAPS_DATA_ROOT

                        if ($REMOTE_DATA_ROOT != "") then
                            cd $OUTDIR_FSF 
                            $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fsf $REMOTE_DATA_ROOT/lapsprd/fsf/$MODEL exchange
                        endif

                    endif

                    ls -l $NCOUT_FSF                

                else # MODE_FUA (lfmregrid method - read GRIB file via FORTRAN routines)

                    echo "lfmregrid method..."
                    echo "$LAPSINSTALLROOT/bin/lfmregrid.exe $GRIB_PRES_IN $YYDDDHHMM $FCSTTIME T F F F wrf-hrrr $LAPS_DATA_ROOT"
                          $LAPSINSTALLROOT/bin/lfmregrid.exe $GRIB_PRES_IN $YYDDDHHMM $FCSTTIME T F F F wrf-hrrr $LAPS_DATA_ROOT 

                    if ($REMOTE_DATA_ROOT != "") then
                        cd $OUTDIR_FUA 
                        $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fua $REMOTE_DATA_ROOT/lapsprd/fua/$MODEL exchange
                    endif

                endif # MODE_FUA (WGRIB method)


            else # missing input data for FUA
                echo "WARNING: $GRIB_PRES_IN or $LAPS_DATA_ROOT/static/Variable_Tables/nctable.HRRR.fua is not present"

                echo " "
                echo "mv $TMPOUT_FSF $NCOUT_FSF"
                      mv $TMPOUT_FSF $NCOUT_FSF

                echo "**** regridding $NCOUT_FSF by inputting $OUTDIR_FSF in arg 1) ****"
                echo "$LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F F T wrf-hrrr $LAPS_DATA_ROOT"
                      $LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F F T wrf-hrrr $LAPS_DATA_ROOT

                ls -l $NCOUT_FSF                

                if ($REMOTE_DATA_ROOT != "") then
                    cd $OUTDIR_FSF 
                    $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fsf $REMOTE_DATA_ROOT/lapsprd/fsf/$MODEL exchange
                endif

            endif # $GRIB_PRES_IN exists

            echo " "
            date -u
            echo " "

        else # off the hour

            echo " "
            echo "mv $TMPOUT_FSF $NCOUT_FSF"
                  mv $TMPOUT_FSF $NCOUT_FSF

            echo "***** regridding $NCOUT_FSF by inputting $OUTDIR_FSF in arg 1) *****"
            echo "$LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F F T wrf-hrrr $LAPS_DATA_ROOT"
                  $LAPSINSTALLROOT/bin/lfmregrid.exe $OUTDIR_FSF/$YYDDDHHMM$FCSTTIME $YYDDDHHMM $FCSTTIME F F F T wrf-hrrr $LAPS_DATA_ROOT

            ls -l $NCOUT_FSF                

            if ($REMOTE_DATA_ROOT != "") then
                cd $OUTDIR_FSF 
                $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fsf $REMOTE_DATA_ROOT/lapsprd/fsf/$MODEL exchange
            endif

        endif # on the hour

      else # other model
        if ($MODEL == rap-nh) then
            setenv FCSTTIME_IN  $FILE_HH\00    
        else
            setenv FCSTTIME_IN  00$FILE_HH    
        endif
        setenv FCSTTIME_OUT $FILE_HH$FMM

        echo " "
        echo "Processing $MODEL $CMODEL with input/output forecast time of $FCSTTIME_IN/$FCSTTIME_OUT"
        echo "lfmregrid method..."

        setenv GRIB_PRES_IN $GRIBINDIR/$YYDDDHHMM$FCSTTIME_IN
        echo "$LAPSINSTALLROOT/bin/lfmregrid.exe $GRIB_PRES_IN $YYDDDHHMM $FCSTTIME_OUT T T F F $MODEL $LAPS_DATA_ROOT"
              $LAPSINSTALLROOT/bin/lfmregrid.exe $GRIB_PRES_IN $YYDDDHHMM $FCSTTIME_OUT T T F F $MODEL $LAPS_DATA_ROOT 

        if ($REMOTE_DATA_ROOT != "") then
            cd $OUTDIR_FUA 
            $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fua $REMOTE_DATA_ROOT/lapsprd/fua/$MODEL exchange
        endif

        if ($REMOTE_DATA_ROOT != "") then
            cd $OUTDIR_FSF 
            $LAPSINSTALLROOT/etc/copyremote.sh $YYDDDHHMM$FCSTTIME.fsf $REMOTE_DATA_ROOT/lapsprd/fsf/$MODEL exchange
        endif

      endif # CMODEL = HRRR

#     exit # for testing

    end # minute loop

   endif # within LAPS forecast time range

  end # hour loop

  echo ""
  echo "output..."
  ls -l $OUTDIR_FSF

end # Loop through models
